package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.CommentInformationDao;
import Entity.Comment;

/**
 * Servlet implementation class CommentServlet
 */
//@WebServlet("/CommentServlet")
public class CommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//解决乱码
		response.setContentType("text/html;charset=utf-8");//解决乱码
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter(); //把编码设置放到输出流获取之前
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String commentID=request.getParameter("commentID");
		String commentatorID=request.getParameter("commentatorID");
		String resourceID=request.getParameter("resourceID");
		String commentTime=request.getParameter("commentTime");
		System.out.println("当前时间为："+commentTime);
		/*
		 * DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date
		 * commentTimeDate=null; try { commentTimeDate = fmt.parse(commentTime); } catch
		 * (ParseException e) { // TODO Auto-generated catch block e.printStackTrace();
		 * } System.out.println("当前时间为："+commentTimeDate);
		 */
		String theContent=request.getParameter("theContent");
		Comment comment=new Comment(commentID,resourceID,commentatorID,theContent,commentTime);
		if(new CommentInformationDao().addComment(comment)>0) {
			response.sendRedirect("downloadLearningResource.jsp?resourceID="+resourceID+"");
			//out.print("<script type='text/javascript'>alert('评论成功！')</script>");
		}
		out.flush();
		out.close();
	}

}
